<?php
###
#  Baing PHP 框架 1.0.0
#  http://baing.co/
#
#  Copyright: 白晓阳
#  License: CC BY-NC-SA 4.0
#  https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh
###

namespace bai\resource\document;

use bai\feature\RuntimeException, bai\feature\ResourceException;
use bai\feature\Paths;
use bai\Resource, bai\resource\application\Logger;


class Temp extends \bai\resource\Document
{
    public const TYPE_DEFAULT = '';

    protected array $runtime = [
        'exclude' => [PHP::TYPE_DEFAULT, Xml::TYPE_DEFAULT, Logger::TYPE_DEFAULT]
    ];

    protected function init()
    {
        $path = $this->fitPath($this->find('path') ?? '');
        $fullPath = Paths::filterPath($path);
        if (!$fullPath) {
            throw new RuntimeException('path', $path);
        }
        $root = self::config(['path', self::className(Resource::class)]);
        if (!str_starts_with($fullPath, $root)) {
            throw new RuntimeException('path', $path);
        }
        $exclude = $this->find('exclude') ?? [];
        foreach ($exclude as $item) {
            if (str_ends_with($fullPath, ".$item")) {
                throw new RuntimeException('exclude', $path);
            }
        }
        if (!is_file($fullPath)) {
            throw new ResourceException(params: $path);
        }
        $this->runtime['path'] = $fullPath;
    }
}